package com.aos.eap.activity;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.BuyoffBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.ParseException;

import butterknife.BindView;
import butterknife.OnClick;

public class QC_BuyoffActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.tvLiaoTiao)
    TextView tvLiaoTiao;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.tvBuyoffId)
    TextView tvBuyoffId;
    @BindView(R.id.tvCreateUser)
    TextView tvCreateUser;
    @BindView(R.id.rbPass)
    RadioButton rbPass;
    @BindView(R.id.rbNoPass)
    RadioButton rbNoPass;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private BuyoffBean buyoffBean;

    private String isPass = "Y";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_qc_buyoff;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etLotId, this, this::requestInfoByEqpId);
    }

    private void requestInfoByEqpId(String lotId) {
        JsonObject map = new JsonObject();
        map.addProperty("LotID", lotId);
        NetUtils.requestGetNet(this, "EMS_BuyOffQcLoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<BuyoffBean>>(
                new TypeToken<BaseResponseBean<BuyoffBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<BuyoffBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    buyoffBean = response.body().getResponse().getBody();
                    setPageData(buyoffBean);
                }else {
                    buyoffBean = null;
                    setPageData(new BuyoffBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BuyoffBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_BuyOffQcLoad");
                buyoffBean = null;
                setPageData(new BuyoffBean());
            }
        });
    }
    private void setPageData(BuyoffBean buyoffBean) {
        tvEqpId.setText(buyoffBean.getEqpID());
        tvBuyoffId.setText(buyoffBean.getOrderID());
        tvCreateUser.setText(buyoffBean.getOrderUser());
        tvLiaoTiao.setText(buyoffBean.getStripLoc());
    }
    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("Buyoff");
        freeUI_titleView.setLeftListener(view -> showBackDialog(QC_BuyoffActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (buyoffBean == null) {
                showToast("未获取到页面信息，请重新扫描设备编号", false);
                etLotId.setText("");
                MyUtils.setEditTextFocus(etLotId);
                return;
            }
            if (FreeApi_StringUtils.isEmpty(buyoffBean.getStripLoc())) {
                showToast("Buyoff尚未送检，不能提交", false);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("EqpID", buyoffBean.getEqpID());
            map.addProperty("LotID", buyoffBean.getLotID());
            map.addProperty("OrderID", buyoffBean.getOrderID());
            map.addProperty("OrderUser", buyoffBean.getOrderUser());
            map.addProperty("StripLoc", buyoffBean.getStripLoc());
            map.addProperty("CheckResult", isPass);
            map.addProperty("Remark", etRemark.getText().toString().trim());
            map.addProperty("QCUser", Constant.CURRENT_USER);
            try {
                map.addProperty("QCTime", FreeApi_DateUtils.longToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_BuyOffQC", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(QC_BuyoffActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_BuyOffQC");
            }
        });
    }

    @OnClick({R.id.rbPass, R.id.rbNoPass})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rbPass:
                isPass = "Y";
                break;
            case R.id.rbNoPass:
                isPass = "N";
                break;
        }
    }
}
